/***************************************************************************
                          cardviewwidget.cpp  -  description
                             -------------------
    begin                : Thu May 8 2008
    copyright            : (C) 2008 by Samuel Harrington
    email                : robotsam@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "cardviewwidget.h"

#include "sqlite3stmt.h"

CardViewWidget::CardViewWidget(QWidget *parent, const char *name ) : QWidget(parent,name) {
  setupUi(this);
}

CardViewWidget::~CardViewWidget(){
}

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void CardViewWidget::languageChange()
{
    retranslateUi(this);
}

void CardViewWidget::load(int n, sqlite3 * sql_) {
  sql = sql_;
  card_t_n = n;

  QString q = "SELECT name, attack, defense, mana, type, text, comments FROM card_t WHERE num="+QString::number(card_t_n)+";";

  {
    sqlite3Stmt s(sql,q);
    if(!s.done()) {
      tlCardName->setText(s.column_text(0));
      QString t = s.column_text(1);
      if(!t.isEmpty()) {
        t += "/";
        t += s.column_text(2);
      }
      leAD->setText(t);
      leMana->setText(s.column_text(3));
      leType->setText(s.column_text(4));
      teOracle->setText(s.column_text(5));
      leComments->setText(s.column_text(6));
    }
  }
  
  q = "SELECT releases.rname";
  q += " FROM card_ver, releases";
  q += " WHERE releases.rnum==card_ver.ver AND card_ver.card=="+QString::number(n)+";";

  cbPrinting->clear();

  for(sqlite3Stmt s(sql,q) ; !s.done() ; s.step()) {
    cbPrinting->insertItem(s.column_text(0));
  }

  load_printing(cbPrinting->currentText());
}

void CardViewWidget::load_printing(const QString & str) {
  const QString q = "SELECT rarity FROM card_ver, releases WHERE rnum=ver and rname='"+str+"' and card="+QString::number(card_t_n)+";";
  for(sqlite3Stmt s(sql,q) ; !s.done() ; s.step()) {
    leRarity->setText(s.column_text(0));
  }
}

void CardViewWidget::refresh() {
  QString q = "SELECT comments FROM card_t WHERE num="+QString::number(card_t_n)+";";

  sqlite3Stmt s(sql,q);
  if(!s.done()) {
    leComments->setText(s.column_text(0));
  }
}

void CardViewWidget::save() {
  const QString s = "UPDATE card_t SET comments = '"+leComments->text().replace('\'',"''")+"' WHERE num="+QString::number(card_t_n)+";";
  if(sqlite3_exec(sql,s,0,0,0))
    qWarning("Could not update comments for card %d(%s): %s",card_t_n,s.ascii(),sqlite3_errmsg(sql));
}

void CardViewWidget::download_image() {
  qWarning("CardViewWidget::download_image() not implemented yet!");
}

